package com.platform.schedule;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.logger.Logger;
import com.github.ltsopensource.core.logger.LoggerFactory;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.logger.BizLogger;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.LtsLoggerFactory;
import com.platform.service.OrderService;
import com.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zhu.xiang on 2018/11/10 0010.
 */
public class JobRunner implements com.github.ltsopensource.tasktracker.runner.JobRunner{
    private static final Logger LOGGER = LoggerFactory.getLogger(JobRunner.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;


    @Override
    public Result run(JobContext jobContext) throws Throwable {
        try {

            if (jobContext.getJob().getTaskId().startsWith("orderAutoCancel_")){
                //订单自动取消
                Integer orderId = Integer.valueOf(jobContext.getJob().getParam("orderId"));
                String orderSn = jobContext.getJob().getParam("orderSn");
                orderService.autoCancelOrderCallback(orderId);
                BizLogger bizLogger = LtsLoggerFactory.getBizLogger();
                // 会发送到 LTS (JobTracker上)
                bizLogger.info("订单号：" + orderSn + "系统自动取消");
                LOGGER.info("订单号：" + orderSn + "系统自动取消");
            } else if (jobContext.getJob().getTaskId().equals("refreshUserLevel")){
                //自动刷新客户级别
                LOGGER.info("自动刷新客户级别");
                userService.refreshUserLevel();
            } else if (jobContext.getJob().getTaskId().equals("refreshUserFreezeAccountJob")){
                //自动刷新客户冻结佣金
                LOGGER.info("自动刷新客户冻结佣金");
                userService.refreshUserFreezeAccount();
            }


        } catch (Exception e) {
            LOGGER.info("Run job failed!", e);
            return new Result(Action.EXECUTE_LATER, e.getMessage());
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了，哈哈");
    }

}
